#!/usr/bin/env python
# Copyright(c) 2010 Nuttachat Wisittipanit. All rights reserved.

from database.template.connecttemplate import  connectTemplate
from mahbo.information.database.utility import mysqlcolumn

import MySQLdb


class dissertationMySQLrecord(connectTemplate):
    
    def __init__(self):
        self.mainDBname     = 'dissertation'
        connectTemplate.__init__(self,self.mainDBname)
        
    def initiateDB(self):
        #MySQL properties for the results
        #info table keeps information to reproduce the data
        self.tab_ms         = mysqlcolumn.MySQLcol_DISSERTATION()
        try:
            self.query('desc %s' % self.tab_ms.tablename)
        except (MySQLdb.ProgrammingError,):
            self.query("""
                create table %s
                (
                    %s int NOT NULL AUTO_INCREMENT,%s CHAR(200),PRIMARY KEY(%s)                    
                )
                ENGINE = MYISAM
                """ % ( self.tab_ms.tablename   ,
                        self.tab_ms.sid         ,
                        self.tab_ms.sname       ,
                        self.tab_ms.sid         ,))
        
                
class dataAnalysisMySQLrecord(connectTemplate):
    
    def __init__(self,mainDBname):
        self.mainDBname     = mainDBname
        connectTemplate.__init__(self,self.mainDBname)
        
    def initiateDB(self):
        #MySQL properties for the results
        #info table keeps information to reproduce the data
        self.tab_info       = mysqlcolumn.MySQLcol_INFO()
        self.tab_results    = mysqlcolumn.MySQLcol_RESULTS()
        self.tab_flookup    = {}
        try:
            self.query('desc %s' % self.tab_info.tablename)
        except (MySQLdb.ProgrammingError,):
            self.query("""
                create table %s
                (
                    %s int NOT NULL AUTO_INCREMENT,%s CHAR(200),%s LONGBLOB,PRIMARY KEY(%s)                    
                )
                ENGINE = MYISAM
                """ % ( self.tab_info.tablename,self.tab_info.nameid,self.tab_info.name,
                        self.tab_info.info_obj,self.tab_info.nameid,) )            
        try:
            self.query('desc %s' % self.tab_results.tablename)
        except (MySQLdb.ProgrammingError,):
            self.query("""
                create table %s
                (
                    %s int NOT NULL AUTO_INCREMENT,%s CHAR(200),%s LONGBLOB,PRIMARY KEY(%s)
                )
                ENGINE = MYISAM
                """ % ( self.tab_results.tablename,self.tab_results.resultid,self.tab_results.datasetname,
                        self.tab_results.result_obj,self.tab_results.resultid,))
            
    def addFLookup(self,tablename):
        try:
            self.tab_flookup[tablename]
        except (KeyError,):
            self.tab_flookup[tablename] = mysqlcolumn.MySQLcol_FLOOKUP(tablename = tablename)
        tf  = self.tab_flookup[tablename] 
        try:
            self.query('desc %s' % tablename)
        except (MySQLdb.ProgrammingError,):
            self.query("""
                create table %s
                (
                    %s CHAR(10),%s TEXT,PRIMARY KEY(%s)
                )
                """ % ( tf.tablename    ,
                        tf.mfeature     ,
                        tf.rfeature     ,
                        tf.mfeature     ,
                        )
                )
            
    
class gutMySQLrecord(connectTemplate):
    
    def __init__(self,mainDBname):
        self.mainDBname     = mainDBname
        connectTemplate.__init__(self,self.mainDBname)
        
    def initiateDB(self):
        #MySQL properties for the results
        self.tab_L      = mysqlcolumn.MySQLcol_TABLEL()
        self.tab_res    = mysqlcolumn.MySQLcol_GUTRESULTS()
        self.tab_resx   = mysqlcolumn.MySQLcol_GUTRESULTSX()
        try:
            self.query('desc %s' % self.tab_L.tablename)
        except (MySQLdb.ProgrammingError,):
            self.query("""
                create table %s
                (
                    %s CHAR(200) ,%s LONGBLOB
                )
                ENGINE = MYISAM
                """ % ( self.tab_L.tablename, self.tab_L.name, self.tab_L.info   ))
        for tab_results in (self.tab_res,self.tab_resx,):
            try:
                self.query('desc %s' % tab_results.tablename)
            except (MySQLdb.ProgrammingError,):
                self.query("""
                    create table %s
                    (
                        %s int NOT NULL AUTO_INCREMENT,%s CHAR(200),%s LONGBLOB,%s LONGBLOB,
                        %s LONGBLOB,%s LONGBLOB,PRIMARY KEY(%s)
                    )
                    ENGINE = MYISAM
                    """ % ( tab_results.tablename,tab_results.phdid,tab_results.datasetname,
                            tab_results.datafact,tab_results.results_svm,tab_results.results_mstat,
                            tab_results.results_som,tab_results.phdid ))
        
            

